package com.ruoyi.controller.admin.plot;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.admin.AdminPlotDB;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.admin.AdminPlotService;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/admin/plot")
public class AdminPlotController extends BaseController {
    @Resource
    private AdminPlotService adminPlotDBService;

    /**
     * 查询后台-菜园列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminPlotDB adminPlotDB) {
        return adminPlotDBService.selectAdminPlotDBList(adminPlotDB);
    }

    /**
     * 导出后台-菜园列表
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:export')")
    @Log(title = "后台-菜园", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AdminPlotDB adminPlotDB) {
        List<AdminPlotDB> list = (List<AdminPlotDB>) adminPlotDBService.selectAdminPlotDBList(adminPlotDB).getRows();
        ExcelUtil<AdminPlotDB> util = new ExcelUtil<AdminPlotDB>(AdminPlotDB.class);
        util.exportExcel(response, list, "后台-菜园数据");
    }

    /**
     * 获取后台-菜园详细信息
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(adminPlotDBService.selectAdminPlotDBById(id));
    }

    /**
     * 新增后台-菜园
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:add')")
    @Log(title = "后台-菜园", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminPlotDB adminPlotDB) {

        return toAjax(adminPlotDBService.insertAdminPlotDB(adminPlotDB));
    }

    /**
     * 修改后台-菜园
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:edit')")
    @Log(title = "后台-菜园", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminPlotDB adminPlotDB) {
        return toAjax(adminPlotDBService.updateAdminPlotDB(adminPlotDB));
    }

    // addPlant

    /**
     * 修改后台-菜园
     */
    @PreAuthorize("@ss.hasPermi('plot:plot:edit')")
    @Log(title = "后台-菜园", businessType = BusinessType.UPDATE)
    @PutMapping("/addPlant")
    public AjaxResult addPlant(@RequestParam(name = "plotId") Long plotId, @RequestParam(name = "cropIds") List<Long> cropIds, @RequestParam(name = "time", required = false) String time) {
        // String time 转为 date
        Date date = DateUtils.getNowDate();
        if (ObjectUtils.isNotEmpty(time)){
             date = DateUtils.parseDate(time);
        }

        adminPlotDBService.addPlant(plotId, cropIds, date);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @GetMapping("/findCropList")
    public AjaxResult  findCropList(@RequestParam(name = "plotId") Long plotId) {
        return AjaxResult.success(adminPlotDBService.findCropList(plotId));
    }

    // 查询未出租菜地列表all
    // TODO 二维码加上地块id
    @PreAuthorize("@ss.hasPermi('plot:plot:list')")
    @GetMapping("/findAll")
    public AjaxResult findAll() {
        return AjaxResult.success(adminPlotDBService.findAll());
    }

}
